﻿using System.Reflection;
using CoreEx.Database.Metadata;

namespace CoreEx.Database.Mapping
{
    /// <summary>
    /// Represents the mapping between a property and a table column.
    /// </summary>
    public class PropertyMappingInfo
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyMappingInfo"/> class.
        /// </summary>
        public PropertyMappingInfo()
        {
            RelationType = CardinalityType.None;
        }

        /// <summary>
        /// Gets or sets the target <see cref="PropertyInfo"/>.
        /// </summary>
        public PropertyInfo Property { get; set; }
     
        /// <summary>
        /// Gets or sets the <see cref="ColumnInfo"/> that represents 
        /// the column that the <see cref="Property"/> is mapped to.
        /// </summary>
        public ColumnInfo ColumnInfo { get; set; }
        
        /// <summary>
        /// Gets or sets the <see cref="ForeignKeyInfo"/> that represents 
        /// the foreign key to another table.
        /// </summary>
        /// <remarks>
        /// If the <see cref="RelationType"/> is <see cref="CardinalityType.None"/> this property will be set to <b>null</b>
        /// If the <see cref="RelationType"/> is <see cref="CardinalityType.OneToOne"/> this property will reference the 
        /// foreign key from the "many" side of the relation. 
        /// </remarks>
        public ForeignKeyInfo ForeignKeyInfo { get; set; }        
        
        /// <summary>
        /// Gets or sets the <see cref="CardinalityType"/> that this property represents.
        /// </summary>
        public CardinalityType RelationType { get; set;}


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            switch (RelationType)
            {
                case CardinalityType.None:
                    return string.Format("{0} => {1}",Property.Name, ColumnInfo.ColumnName);
                case CardinalityType.OneToOne:
                case CardinalityType.ManyToOne:
                case CardinalityType.ManyToMany:
                case CardinalityType.OneToMany:
                    return string.Format("{0} => {1}", Property.Name, ForeignKeyInfo.Name);
            }
            return base.ToString();
        }
    }
}
